package com.nsfocus.file;

import java.io.*;
import java.util.ArrayList;

import javax.swing.*;

/**
 * @File: ObjectStreanDemo.java
 * @User: Liangkangkang
 * @Date: 17-4-23
 * @Time: 下午10:27
 * @Mail: liangkangkang@yahoo.com
 * @Desc:
 * @Version:
 */
public class ObjectStreanDemo {
    public static void main(String[] args) throws Exception {
        FileOutputStream   fos = new FileOutputStream("/home/lkk/4-22");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(new String("hello world"));
        oos.close();
        fos.close();
        System.out.println("---------------");

        // read file context
        FileInputStream   fis = new FileInputStream("/home/lkk/4-22");
        ObjectInputStream ois = new ObjectInputStream(fis);
        String            str = (String) ois.readObject();

        System.out.println(str);
        System.out.println("---------------");

        // 字节数组输入输出流
        // 写入(串行)
        ByteArrayOutputStream bos  = new ByteArrayOutputStream();
        ObjectOutputStream    oos1 = new ObjectOutputStream(bos);

        oos1.writeObject(new String("liangkangkang"));
        oos1.writeObject(new Integer(1000));

        Dog dog = new Dog("xiao", 3);

        oos1.writeObject(dog);
        bos.close();
        oos1.close();
        Dog g = new Dog("ccc",3);

        // 读取(反串行)
        byte[]               bytes = bos.toByteArray();
        ByteArrayInputStream bis   = new ByteArrayInputStream(bytes);
        ObjectInputStream    ois1  = new ObjectInputStream(bis);
        Object               o     = ois1.readObject();

        System.out.println(o);
        o = ois1.readObject();
        System.out.println(o);

        // 关键是串行序列化
        Dog d = (Dog) ois1.readObject();

        System.out.println(d.getName());
        String cc = "ysl";
        System.out.println("---------------");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
